﻿using AutoMapper;
using Dm.filter.log;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Opc.Ua;
using System.IO.Compression;
using System.ServiceModel.Channels;
using System.Threading.Tasks;
using Warehouse.Entites;
using Warehouse.Services.Repository;
using Warehouse.ViewModels.TESinterfaceDTO;
using WCS.Entities;
using WCS.Entities.Api;
using WCS.ViewModels.TESinterfaceDTO;

namespace Juionwarehouse.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class apiv2Controller : ControllerBase
    {
        private readonly Repository<Handling> hand; //仓储里面要用实体类
        private readonly Repository<Siterelease> sit;
        private readonly Repository<Redatport> red;
        private readonly IMapper mapper;
        public apiv2Controller(Repository<Handling> hand, Repository<Siterelease> sit, IMapper mapper, Repository<Redatport> red)
        {
            this.hand = hand;
            this.sit = sit;
            this.mapper = mapper;
            this.red = red;
        }
        //接收状态信息
        [HttpPost]
        [AllowAnonymous]
        public ApiResult Redatport(RedatportDTO model)
        {
            if (model != null && model.messageType == 10)
            {
                var Redatpor = new Redatport()
                {
                    content = model.ToString(),
                    podInfo = model.ToString(),
                    createTime = model.createTime,
                    messagelD = model.messagelD,
                    messageType = model.messageType,
                    warehouseID = model.warehouseID,
                    clientCode = model.content.clientCode,
                    taskID = model.content.taskID,
                    bizID = model.content.bizID,
                    priority = model.content.priority,
                    taskType = model.content.taskType,
                    status = model.content.status,
                    errorCode = model.content.errorCode,
                    errorReason = model.content.errorReason,
                    robotID = model.content.robotID,
                    podID = model.content.podID,
                    desType = model.content.desType,
                    desNodeID = model.content.desNodeID,
                    desStorageID = model.content.desStorageID,
                    desZoneCode = model.content.desZoneCode,
                    desStationCodes = model.content.desStationCodes,
                    createTimes = model.content.createTime,
                    finishTime = model.content.finishTime,
                    positionType = model.content.podInfo.positionType,
                    nodeID = model.content.podInfo.nodeID,
                };
                red.Add(Redatpor);
                return ApiResultHelp.Success(model);
            }
            else
            {
                return ApiResultHelp.Error("任务失败");
            }
            //return ApiResultHelp.Error("任务成功");
        }

        /// <summary>
        /// 搬运任务完成
        /// </summary>
        /// <returns></returns>
        //[HttpPost]
        //public ApiResult TasksMov(HandlingCompletedDTO model)
        //{

        //    if (model.messageType == 10)
        //    {
        //        var h = mapper.Map<Handling>(model);
        //        hand.AddAsync(h);
        //    }
        //    else
        //    {
        //        return ApiResultHelp.Error("错误任务未完成");
        //    }

        //    return ApiResultHelp.Success("任务完成");
        //}

        /// <summary>
        /// 站点容器释放
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult releaseStation(SiteReleaseDTO model)
        {

            if (model!=null)
            {
                var sitere = new Siterelease()
                {
                    content = model.ToString(),
                    messageID=model.messageID,
                    warehouseID=model.warehouseID,
                    createTime = model.createTime,
                    messageType = model.messageType,
                    podID = model.content.podID,
                    stationCode=model.content.stationCode,
                    occupyStatus = model.content.occupyStatus
                };

                sit.Add(sitere);
            }
            else
            {
                return ApiResultHelp.Error(500, "错误释放失败");
            }
            return ApiResultHelp.Success("站点以释放");
        }

    }
}
